package com.atguigu.wordcount.wccombine;

import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * @ClassName WcMapper
 * @DeScription WcMapper
 * @Author 剧情再美终是戏
 * @Date 2019/11/16 14:48
 * @Version 1.0
 **/
public class WcMapper extends Mapper<LongWritable, Text, Text, IntWritable> {

    // 设置输出参数
    private Text outKey = new Text();
    private IntWritable outVal = new IntWritable(1);

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

        // 参数拦截
        if (null == value) {
            return;
        }

        // 获取一行的内容
        String line = value.toString();

        // 以空格为分割
        if (StringUtils.isNotBlank(line)) {
            String[] split = line.split(" ");
            if (null == split || split.length < 1) {
                return;
            }

            // 循环将结果写出
            for (String sp : split) {
                outKey.set(sp);
                context.write(outKey, outVal);
            }
        }
    }
}
